<?php

class HostController extends ControllerModuleWhm
{
	public function actionIndex()
	{
		$hostAddForm = new HostAddForm();
		$host = new Host();
		
		$postData = app()->request->getPost('HostAddForm', null);
		if($postData){
			$hostAddForm->attributes = $postData;
			$ret = false;
			$detail = $errors = null;
			if($hostAddForm->validate()){
				$host->attributes = $postData;
				if(!($ret = $host->save(true))){
					$errors = $host->getErrors();
				}else{
					app()->__Whatever__Log->recordHostAction(SystemLog::HOST_ADD, $host->name, $host->id_host);
					
					$detail = $postData;
					$detail['id_host'] = $host->getPrimaryKey();
				}
			}else{
				$errors = $hostAddForm->getErrors();
			}
			
			$this->_returnAjax(
				array(
					'result' => $ret,
					'detail' => $detail,
					'errors' => $errors
				)
			);
		}
		
		$this->render('index', array(
			'hostAddForm' => $hostAddForm,
			'hostEditForm' => new HostEditForm(),
			'host' => $host
		));
	}
	
	public function actionDetail($id_host)
	{
		$id_host = app()->request->getParam('id_host', null);
		if($id_host){
			if(app()->request->isAjaxRequest){
				$host = $this->loadModelByPk($id_host, 'Host');
				$this->_returnAjax(
					$host->attributes
				);
			}
		}
	}
	
	public function actionEdit()
    {
        $postData = app()->request->getParam('HostEditForm', null);
    	if($postData){
//    		$postData = array_filter($postData);
	        $model = $this->loadModelByPk(empty($postData['id_host'])? '' : $postData['id_host'], 'Host');
	
	        if($model){
	            $model->attributes = $postData;
	            if(app()->request->isAjaxRequest){
	            	$ret = $model->save();
	            	if($ret){
	            		app()->__Whatever__Log->recordHostAction(SystemLog::HOST_EDIT, $model->name, $model->id_host);
	            	}
	            	
	            	$this->_returnAjax(array(
	            		'result' => $ret,
	            		'detail' => $model->attributes,
	            		'errors' => $model->getErrors()
	            	));
	            }else{
	            	if($model->save()){
	                	// redirect
	            	}
	            }
	        }
	
	        $this->render('edit', array(
	            'model' => $model,
	        ));    		
    	}
    }
	
	public function actionDelete()
	{
		if(app()->request->isAjaxRequest){
			$id_host = app()->request->getPost('id_host', null);
			if($id_host){
				$host = $this->loadModelByPk($id_host, 'Host');
				$ret = $host->delete();
				if($ret){
					app()->__Whatever__Log->recordHostAction(SystemLog::HOST_DELETE, $host->name, $host->id_host);
				}
				
				$this->_returnAjax(array(
					'result' => $ret,
					'errors' => $host->getErrors()
				));
			}
		
			$this->_end();
		}
	}
	
	public function actionLoginDetail()
	{
		$hostLogin = new HostLogin();
		$id_account = app()->request->getParam('id_account', null);
		$preSelectedHostId = '';
		if($id_account){
			$whmaccount = WhmAccount::model()->findByPk($id_account);
			if($whmaccount->Host){
				$preSelectedHostId = $whmaccount->Host->id_host;
			}
		}else{
			$preSelectedHostId = app()->request->getParam('id_host', '');
		}
		
		$postData = app()->request->getPost('HostLogin', null);
		if($postData){
			if(app()->request->isAjaxRequest){
				HostLogin::model()->deleteAllByAttributes(array('id_host' => $postData['id_host']));
				
				$hostLogin->attributes = $postData;
				$this->_returnAjax(array(
					'result' => $hostLogin->save(),
					'errors' => $hostLogin->getErrors()
				));
			}
		}
		
		$this->render('logindetail', array(
			'hostLogin' => $hostLogin,
			'preSelectedHostId' => $preSelectedHostId
		));
	}
	
	public function actionGetLoginDetail()
	{
		$id_host = app()->request->getParam('id_host', null);
		if($id_host){
			if(app()->request->isAjaxRequest){
				$hostLogin = HostLogin::model()->findByAttributes(array('id_host' => $id_host));
				$ret = null;
				if(!empty($hostLogin)){
					$hostLogin->password = $hostLogin->getPlainPassord();
					$ret = $hostLogin->attributes;
				}
				
				$this->_returnAjax($ret);
			}
		}
	}
	
	public function actionLogin()
	{
		$this->layout = '//layouts/login';
		
		$id_host = app()->request->getParam('id_host', null);
		if($id_host){
			$host = Host::model()->findByPk($id_host);
			if($host->HostLogin){
				app()->__Whatever__Log->recordHostAction(SystemLog::HOST_DIRLOGIN, $host->name, $host->id_host);
				
				$this->render('login', array(
					'HostLogin' => $host->HostLogin
				));
			}
		}
	}
	
	public function accessRules()
	{
		return array(
			array(
				'allow',
				'actions' => array('index', 'detail', 'edit', 'delete', 'logindetail', 'getlogindetail', 'login'),
				'expression' => 'app()->user->isLogin() && app()->user->hasManageHostPerm()',
				'redirect' => array('default/noaccess')
			),

			array(
				'deny',	//should be 'deny', 'allow' is used to give convenient to make test
				'users' => array('*'),
				'redirect' => array('user/login')
			)
		);
	}
}